import json
import requests
import time

headers = {
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://sam.gov/search/?page=10&pageSize=25&sort=-modifiedDate&index=opp&sfm%5BsimpleSearch%5D%5BkeywordRadio%5D=ANY&sfm%5BsimpleSearch%5D%5BkeywordTags%5D%5B0%5D%5Bkey%5D=Supply&sfm%5BsimpleSearch%5D%5BkeywordTags%5D%5B0%5D%5Bvalue%5D=Supply&sfm%5Bstatus%5D%5Bis_active%5D=true",
    "sec-ch-ua": '"Chromium";v="142", "Microsoft Edge";v="142", "Not_A Brand";v="99"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0",
    # 'cookie': '_gid=GA1.2.441305154.1763867743; domains=%7B%221%22%3A%22Contract%20Data%22%2C%222%22%3A%22Contract%20Opportunities%22%2C%225%22%3A%22Federal%20Hierarchy%22%2C%227%22%3A%22Wage%20Determinations%22%2C%229%22%3A%22Assistance%20Listing%22%2C%2210%22%3A%22Admin%22%2C%2217%22%3A%22Exclusions%22%2C%2218%22%3A%22Entity%20Registration%22%2C%2219%22%3A%22Entity%20Compliance%22%2C%2220%22%3A%22Customer%20Service%22%2C%2224%22%3A%22System%20Accounts%22%7D; alertsDismiss=597%2C695; MYSESSIONID=451AC81AE6C11B187FE1C395A62F0945; _gat_UA-193079889-1=1; _ga=GA1.1.1657414264.1762612170; _ga_1TZM4G6B9F=GS2.1.s1763867743$o2$g1$t1763869147$j32$l0$h0; _ga_CSLL4ZEK4L=GS2.1.s1763867764$o2$g1$t1763869148$j31$l0$h0; lastVisitedRoute=%2Fsearch%2F%3Fpage%3D1%26pageSize%3D25%26sort%3D-modifiedDate%26index%3Dopp; _dd_s=rum=0&expire=1763870047823',
}
params = {
    "random": str(int(time.time())),
    "index": "opp",
    "page": "199",
    "sort": "-modifiedDate",
    "size": "50",
    "mode": "search",
    "responseType": "json",
    "q": "supply or procurement",
    "qMode": "ANY",
    "is_active": "true",
}
# 10000/50=200
response = requests.get(
    "https://sam.gov/api/prod/sgs/v1/search/",
    params=params,
    headers=headers,
)
result=response.json()
embedded=result['_embedded']
results = embedded["results"]
for result in results:
    print(json.dumps(result, indent=4))
    break

# page=result['page']
# total = result["totalPages"]
# print(page)
# print(result)
# for i in range(1, total):
#     params["page"] = str(i)
#     response = requests.get(
#         "https://sam.gov/api/prod/sgs/v1/search/",
#         params=params,
#         headers=headers,
#     )
#     result = response.json()
#     print(result)
